import { computed, ref } from '@vue/composition-api';
import currency from 'currency.js';
// type  10-30:实物订单，10:商品订单 ,31:充值订单，50-59:卡劵订单，80-89:预付费订单，90:计费订单'
class BuyObject {
    goodsInfo: {};
    sku: {};
    defaultSku: {};
    constructor(value) {
        this.goodsInfo = value;
        if (this.default) {
            this.sku = {
                ...this.default,
                totalPrice: this.default.salePrice,
                sum: this.default.min
            };
        }
    }

    get default() {
        this.defaultSku = this.skuMeta.find((item) => item.default === 1 && item.stock >= item.min) ?? null;
        if (!this.defaultSku) {
            this.defaultSku = this.skuMeta.find((item) => item.stock >= item.min) ?? null;
        }
        return this.defaultSku;
    }

    get skuMeta() {
        return (
            this.goodsInfo.skuMeta.map((item) => {
                item.disable = item.stock === 0 || item.stock < item.min;
                item.totalPrice = item.salePrice;
                item.sum = item.min;
                item.buyMin = item?.min ?? 1;
                item.buyMax = item?.stock !== 0 ? (item?.max >= item?.stock || item?.max === 0 ? item.stock : item.max) : 0;
                return item;
            }) ?? []
        );
    }

    get sum() {
        return this.sku?.sum ?? 1;
    }

    set sum(value) {
        if (this.sku?.sum) this.sku.sum = Number(value);
    }

    changeSum(vlaue) {
        if (this.sku) {
            this.sku.sum = Number(vlaue);
        }
    }

    get min() {
        return this.sku?.min ?? 1;
    }

    get max() {
        if (this.sku?.stock && this.sku?.stock !== 0) return this.sku?.max >= this.sku?.stock || this.sku?.max === 0 ? this.sku?.stock : this.sku?.max;
        return 0;
    }

    changeSku(skuItem) {
        if (!skuItem.disable) this.sku = skuItem;
    }

    get totalPrice() {
        if (this.sku) {
            this.sku.totalPrice = currency(this.sku?.salePrice ?? 0).multiply(this.sku?.sum ?? 1).value;
            return this.sku.totalPrice;
        }
        return 0;
    }

    has(value) {
        return value.sku?.title === this.sku?.title && this.goodsInfo.id === value.goodsInfo.id;
    }

    toString() {
        return this.totalPrice;
    }
}
class CardSet extends Set {
    get totalPrice() {
        const iterator = this.values();
        const result = [...iterator].reduce((accumulator, currentValue) => currency(accumulator).add(Number(currentValue)).value, 0);
        return result;
    }

    isSuperset(value) {
        const iterator = this.values();
        return [...iterator].some((item) => value.has(item));
    }
    toString() {
        return this.totalPrice;
    }
}
export function useCart() {
    const buyCart = ref(new CardSet());
    const buyItem = ref(null);
    const sum = computed({
        get: () => {
            return buyItem.value?.sku?.sum ?? 0;
        },
        set: (value: number | null) => {
            if (buyItem.value?.sku) buyItem.value.sku.sum = Number(value);
        }
    });
    const selectItem = (value) => {
        buyItem.value = new BuyObject(value);
    };
    const addCart = (value) => {
        if (!buyCart.value.isSuperset(value)) buyCart.value.add(value);
    };
    const changeSku = (skuItem) => {
        if (buyItem.value && !skuItem.disable) buyItem.value.sku = skuItem;
    };

    return {
        buyCart,
        buyItem,
        sum,
        selectItem,
        addCart,
        changeSku
    };
}
